-- CALC_MONSTER_ATTACK
-- 地牢怪物的攻击力

return function(monster)
    local attack = monster.dbase:query("attack", 0);

    local combine = PropM.combine;
    local apply = PropM.apply;
    local fetchProps = PropM.fetchProps;
    local trigger = PropM.trigger;

    -- --------------- 攻击增益 ----------------------------------------------
    -- --------------- 先计算固定，再计算百分比  ------------------------------
    -- 攻击+N%
    local prop = combine(monster, "attrib", "attack");
    attack = apply(prop, attack);

    -- 建筑辅助
    prop = PropM.getBuildingAidsProp(monster, "aid_attrib", "attack");
    attack = apply(prop, attack);

    -- 场上有某种怪物时，攻击百分比提升
    local props = fetchProps(monster, "aid_attack1");
    for _, prop in ipairs(props) do
        if prop[3] > 0 then
            attack = trigger(monster, prop[1], prop[2], attack);
        end
    end

    -- 生命较低时，攻击百分比提升
    props = fetchProps(monster, "hp_to_attack");
    local addon = 0;
    for _, prop in ipairs(props) do
        addon = math.max(addon, trigger(monster, prop[1], prop[2], attack));
    end
    attack = attack + addon;

    -- 每损失一定的生命值，攻击百分比提升
    props = fetchProps(monster, "hp_to_attack2");
    local addon = 0;
    for _, prop in ipairs(props) do
        addon = math.max(addon, trigger(monster, prop[1], prop[2], attack));
    end
    attack = attack + addon;

    -- 生命较高时，攻击百分比提升
    props = fetchProps(monster, "hp_to_attack3");
    local addon = 0;
    for _, prop in ipairs(props) do
        addon = math.max(addon, trigger(monster, prop[1], prop[2], attack));
    end
    attack = attack + addon;

    -- 每多一个同类，攻击百分比提升
    props = fetchProps(monster, "resonance");
    for _, prop in ipairs(props) do
        if prop[2] == "attack" then
            attack = trigger(monster, prop[1], "attack", attack);
        end
    end

    -- 每死一个怪物，攻击百分比提升
    props = fetchProps(monster, "violent");
    for _, prop in ipairs(props) do
        if prop[2] == "attack" then
            attack = trigger(monster, prop[1], "attack", attack);
        end
    end

    -- 场上只有自己时，攻击百分比提升
    prop = combine(monster, "single_attrib", "attack");
    if prop[3] > 0 and 1 == #FormulaM.getAliveMonsters(true) then
        attack = attack + apply(prop, attack);
    end

    -- 攻击+N
    attack = attack + monster.dbase:queryTemp("attack_addon", 0);

    -- 攻击+N
    prop = combine(monster, "attrib2", "attack");
    attack = apply(prop, attack);

    -- --------------- 攻击减益 ----------------------------------------------
    -- --------------- 增益后的攻击作为基础，先计算百分比，再计算固定  ----------

    -- 若怪物有忽视defense属性，则减攻无效，直接返回加成后的攻击
    prop = combine(monster, "ignore_defense", 1);
    if apply(prop, 0) > 0 then
        return attack;
    end

    -- 攻击-N%
    prop = combine(monster, "reduce_attrib", "attack");
    attack = apply(prop, attack);

    -- 被玩家”龙珠“属性，百分比削减
    if monster.dbase:query("dragon") then
        props = fetchProps(ME.user, "dragon");
        for _, prop in ipairs(props) do
            attack = apply(prop, attack);
        end
    end

    -- 每死一个怪物，攻击百分比降低
    props = fetchProps(monster, "timid");
    for _, prop in ipairs(props) do
        -- 加攻击的
        if prop[2] == "attack" then
            attack = trigger(monster, prop[1], "attack", attack);
        end
    end

    -- 被变形了，攻击百分比降低
    prop = combine(monster, "transfiguration", 1);
    attack = apply(prop, attack);

    -- 被诅咒了，攻击百分比降低
    props = fetchProps(monster, "curse");
    for _, prop in ipairs(props) do
        attack = apply(prop, attack);
    end

    -- 腐蚀状态，攻击百分比降低
    props = fetchProps(monster, "corriosion");
    for _, prop in ipairs(props) do
        attack = apply(prop, attack);
    end

    -- 黑暗原力，攻击百分比降低
    prop = combine(monster, "dark_force", "attack");
    attack = apply(prop, attack);

    -- 玩家百分比降低敌人攻击
    prop = combine(ME.user, "reduce_attack", 1);
    attack = apply(prop, attack);

    -- 精英怪攻击百分比降低
    attack = FormulaM.invoke("CALC_ELITE_REDUCE_ATTRIB", monster, "attack", attack);

    -- 远程怪攻击降低，放中间
    if monster:isRemoteMonster() then
        -- 百分比削减
        prop = combine(ME.user, "ranged_damage", 1);

        -- 阵营百分比削减
        local pet = ME.user:getActivePet();
        local classId = pet.classId;
        local campId = CampM.getPetCamp(classId);
        local p = combine(ME.user, "camp_ranged_damage", campId);
        prop[3] = apply(p, prop[3]);

        attack = apply(prop, attack);

        -- 绝对值削减
        prop = combine(ME.user, "ranged_damage2", 1);
        attack = apply(prop, attack);
    end

    -- 攻击-N
    prop = combine(monster, "reduce_attrib2", "attack");
    attack = apply(prop, attack);

    -- 玩家降攻属性
    local defense = FormulaM.invoke("CALC_USER_DEFENSE", ME.user);
    if attack > 0 and defense > 0 then
        -- 怪物有攻击，且玩家有该属性
        -- 最少减到1点
        attack = iif(attack > defense, attack - defense, 1);
    end

    return attack;
end
